/**
 * Created by darkleo on 2017/8/21.
 */
function post(url,params,successFun,errorFun,setting) {

    var defaultSetting = {
        async:true,
        showLoading:true,
        showSucMsg:true,
        showErrorMsg:true,
        successMsg:'操作成功',
        errorMsg:'操作失败'
    };
    if(setting){ $.extend(defaultSetting,setting); }
    var loadingIndex = -1;
    if(defaultSetting.showLoading){
         loadingIndex = layer.load(1, {
            shade: [0.1,'#fff']
        });
    }
    $.ajax({
        type:'post',
        url:url,
        async:defaultSetting.async,
        data:params,
        success:function(data){
            layer.close(loadingIndex);
            if(data.code ==2){
                window.location.href="/login.html";
                return false;
            }
            if(data.code ==1){
                if(defaultSetting.showSucMsg){
                    layer.msg(defaultSetting.successMsg)
                }
                if(successFun){ successFun(data.obj); }
            }else{
                if(defaultSetting.showErrorMsg){
                    layer.msg(data.msg||defaultSetting.errorMsg);
                }
                if(errorFun){ errorFun(data.obj, data); }
            }
        },
        error:function(){
            layer.close(loadingIndex);
            layer.msg("操作失败!");
            if(errorFun){ errorFun(); }
        }
    })
}
function clearPost(url,params,successFn,setting){

    var defaultSetting = {
        async:true,
        showLoading:false,
        showSucMsg:false,
        showErrorMsg:false,
        successMsg:'操作成功',
        errorMsg:'操作失败'
    };
    if(setting){ $.extend(defaultSetting,setting); }
    post(url,params,successFn,function(){},defaultSetting);
}